/**
 * $Id$
 *
 * Copyright (c) 2014 ChinaSoft International, Ltd. All rights reserved
 * JointForce Project
 *
 */
package com.csi.jf.crucible.api.stash;

import java.io.File;
import java.io.IOException;

import com.chinasofti.ro.bizframework.core.libs.Base64;
import com.csi.jf.crucible.api.crucible.RestClient;
import com.icss.j2ee.util.http.HttpClientException;

/**
 * @Title: ApiTestProject.java
 * @Description: <br>
 *               <br>
 * @Company: Chinasofti
 * @Created on 2014-7-4 下午3:25:41
 * @author zhangmh
 * @version $Revision: 1.0 $
 * @since 1.0
 */
public class ApiTestProject {
    
    public static final String STASH_HOST = "http://115.29.76.124:7990";
    private String projectKey = "JF-PTS-PRJ";
    private String authorization = "Basic "+Base64.encode("jfpts@chinasofti.com:123456".getBytes());
    private RestClient client = new RestClient(STASH_HOST,authorization);
    
    public static void main(String[] args) throws Exception{
        ApiTestProject test = new ApiTestProject();
        //test.testProjectCreateApi();
        //test.testProjectView();
        //test.testProjectFile();
        test.testProjectUpdate();
    }
    
    /**
     * create project
     * @throws HttpClientException
     * @throws IOException
     * @return
     * {
           "response": {
                    "code": "200", 
                    "message": "operation succeeded", 
                    "cause": ""
           }
       }
     */
    public void testProjectCreateApi() throws HttpClientException, IOException{
        //Adds repository
        String url = "/rest/api/1.0/projects";
        String json = "{"+
            "\"key\": \""+projectKey+"\","+
            "\"name\": \"JF_PTS_Service_Project\","+
            "\"description\": \"JointForce PTS 服务专用Git工程\""+//,"+
            //"\"avatar\": \"data:image/png;base64,<base64-encoded-image-data>\""+
        "}";
        client.doPost(url,json,false);
        /*
         * {"key":"JF-PTS-PRJ","id":346,"name":"JF_PTS_Service_Project","description":"JointForce PTS ????Git??","public":false,"type":"NORMAL","isPersonal":false,"link":{"SONAR_URL":"/projects/JF-PTS-PRJ","rel":"self"},"links":{"self":[{"href":"https://git.jointforce.com/projects/JF-PTS-PRJ"}]}}
         */
    }
    
    public void testProjectUpdateApi() throws HttpClientException, IOException{
        //Adds repository
        String url = "/rest/api/1.0/projects";
        String json = "{"+
            "\"key\": \""+projectKey+"\","+
            "\"name\": \"JF_PTS_Service_Project\","+
            "\"description\": \"The description for my cool project.\""+//,"+
            //"\"avatar\": \"data:image/png;base64,<base64-encoded-image-data>\""+
        "}";
        client.doPut(url,json,false);
    }
    
    public void testProjectView()throws Exception{
        String url = "/rest/api/1.0/projects/"+projectKey;
        client.doGet(url,false);
    }
    
    public void testProjectDelete()throws Exception{
        String url = "/rest/api/1.0/projects/"+projectKey;
        client.doDelete(url,false);
    }
    
    public void testProjectListApi() throws HttpClientException, IOException{
        String url = "/rest/api/1.0/projects?name=&permission=";
        client.doGet(url,false);
    }
    
    public void testProjectFile() throws HttpClientException, IOException{
    	File file=new File("D:\test.doc");
    	String url="/rest/api/1.0/projects/"+projectKey+"/avatar.png";
    	client.doGet(url, false);
    }
    
    public void testProjectUpdate() throws HttpClientException, IOException{
        //Adds repository
    	File file=new File("D:\test.doc");
        String url = "/rest/api/1.0/projects";
        String json = "{"+
            "\"key\": \""+projectKey+"\","+
            "\"name\": \"JF_PTS_Service_Project\","+
            "\"description\": \"The description for my cool project.\""+//,"+
            "\"avatar\": \""+file+"\""+
        "}";
        client.doPut(url,json,false);
    }
    
}
